package com.itfreer.gather.main.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.GeometryEngine;
import com.esri.arcgisruntime.geometry.GeometryType;
import com.esri.arcgisruntime.geometry.Polygon;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.itfreer.arcgis.draw.DrawToolManager;
import com.itfreer.arcgis.utils.MapUtils;
import com.itfreer.gather.R;
import com.itfreer.ui.base.TextView;

/**
 * 定义测量部件
 */
public class MeasureFragment extends Fragment {

    private TextView info;
    private TextView close;
    private TextHandler handler;
    private boolean exit = true;
    private MapView mapView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_fragment_measure, container, false);
        info = view.findViewById(R.id.main_measure_text);
        close = view.findViewById(R.id.main_measure_close);
        handler = new TextHandler();

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit = false;
                DrawToolManager.GetCurrentTool().Clear();
                MeasureFragment.this.getActivity().getFragmentManager().popBackStack();
            }
        });

        return view;
    }

    /**
     * 开启
     */
    public void start(MapView mapView){
        this.mapView = mapView;
        exit = true;
        Thread showThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (exit) {
                    if(DrawToolManager.GetCurrentTool()==null){
                        exit = false;
                    }

                    Geometry geometry = DrawToolManager.GetCurrentTool().GetGeometry();
                    if (geometry != null && !geometry.isEmpty()) {
                        if(geometry.getSpatialReference() == null) {
                            geometry = GeometryEngine.project(geometry, mapView.getSpatialReference());
                        }

                        if(geometry.getGeometryType()== GeometryType.POLYLINE) {
                            Double lengthMi = GeometryEngine.length((Polyline)geometry);
                            lengthMi = (double)Math.round(lengthMi*100)/100l;
                            Message msg = new Message();
                            Bundle b = new Bundle();
                            b.putString("message", lengthMi + "米");
                            msg.setData(b);
                            handler.sendMessage(msg);
                        }
                        else if(geometry.getGeometryType()== GeometryType.POLYGON) {
                            Double mjMi = GeometryEngine.area((Polygon) geometry);
                            mjMi = Math.abs(mjMi); //单位平方米
                            mjMi = mjMi / 10000; //单位公顷
                            mjMi = (double)Math.round(mjMi*10000)/10000l; // 4位小数

                            Message msg = new Message();
                            Bundle b = new Bundle();
                            b.putString("message", mjMi + "公顷");
                            msg.setData(b);
                            handler.sendMessage(msg);
                        }
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        showThread.start();
    }

    private class TextHandler extends Handler {
        public TextHandler() {
        }

        // 子类必须重写此方法，接受数据
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle b = msg.getData();
            String s = b.getString("message");
            info.setText(s);
        }
    }
}
